package com.yeshine.opendata.controller.front.my.app;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.App;
import com.yeshine.opendata.entity.AppTheme;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.dto.StatisticsResult;
import com.yeshine.opendata.service.IAppService;
import com.yeshine.opendata.service.IAppThemeService;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.mybatis.DashboardMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by taohj on 2014/8/1.
 */
@Controller
@RequestMapping("/front/my/app")
public class MyAppController extends AbstractFrontControllerBase {

    @Autowired
    private IAppService service;

    @Autowired
    private IAppThemeService themeService;

    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "order", required = false) String order
    ) {

        PageResult<App> pageResult = new PageResult<App>(page, size);
        if (StringUtils.hasLength(order)) {
            pageResult.setSortBy(order);
            pageResult.setSortColumn("app.name");
        }
        App app = new App();
        if (StringUtils.hasLength(name)) {
            app.setName(name);
        }
        if (theme != null) {
            app.setTheme(new AppTheme(theme));
        }
        app.setCreater(getCurrentUser());
        pageResult.setT(app);
        Result<App> result = service.getByPage(pageResult);

        Result<StatisticsResult> themeList = service.getGroupByTheme(app);

        setRequestAttr("app", app);
        setRequestAttr("order", order);
        setRequestAttr("theme", theme);
        setRequestAttr("result", result);
        setRequestAttr("themeList", themeList);
        return  "/front/my/app/list";
    }



    @RequestMapping("createV")
    public String createV(
    ) {
        List<DashboardMenu> dashboardMenus=new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("新增APP",true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return  "/front/my/app/create";
    }

    @RequestMapping("create")
    public String create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "platform", required = false) Integer platform,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "iamge", required = false) MultipartFile image,
            @RequestParam(value = "developer", required = false) String developer,
            @RequestParam(value = "developerPhone", required = false) String developerPhone,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "linkdata", required = false) String linkdata
    ) {
        App app = new App();
        app.setName(name);
        app.setUrl(url);
        app.setTheme(new AppTheme(theme));
        app.setPlatform(new SysDictionary(platform));
        app.setDescription(description);
        app.setStorage(new SysDictionary(storage));
        app.setVisibility(new SysDictionary(visibility));
        app.setTags(tags);
        app.setFile(file);
        app.setImageFile(image);
        app.setDeveloper(developer);
        app.setDeveloperPhone(developerPhone);
        app.setVersion(version);
        app.setLinkdata(linkdata);


        app.setAuditStatus(new SysDictionary(181));
        app.setStatus(new SysDictionary(111));

        app.setCreater(getCurrentUser());
        app.setCreateTime(new Date());
        Result<App> result = service.create(app, getRequest());
        return getReturnString(result, "redirect:/front/my/app/list", setMessage("操作失败", result.getMessage()));
    }

    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        App app = service.getById(id).getTarget();
        setRequestAttr("app", app);
        setRequestAttr("themes", themeService.getList(null).getList());
        setRequestAttr("platforms", dictionaryService.getList(new SysDictionary("APP_PLATFORM")).getList());
        setRequestAttr("visibilitys", dictionaryService.getList(new SysDictionary("VISIBILITY")).getList());

        List<DashboardMenu> dashboardMenus=new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("更新APP",true));
        setRequestAttr("dashboardMenus", dashboardMenus);

        return  "/front/my/app/modify";
    }

    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "url", required = false) String url,
            @RequestParam(value = "theme", required = false) Integer theme,
            @RequestParam(value = "platform", required = false) Integer platform,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "storage", required = false) Integer storage,
            @RequestParam(value = "tags", required = false) String tags,
            @RequestParam(value = "visibility", required = false) Integer visibility,
            @RequestParam(value = "file", required = false) MultipartFile file,
            @RequestParam(value = "iamge", required = false) MultipartFile image,
            @RequestParam(value = "developer", required = false) String developer,
            @RequestParam(value = "developerPhone", required = false) String developerPhone,
            @RequestParam(value = "version", required = false) String version,
            @RequestParam(value = "linkdata", required = false) String linkdata
    ) {

        App app = service.getById(id).getTarget();
        app.setName(name);
        app.setUrl(url);
        app.setTheme(new AppTheme(theme));
        app.setPlatform(new SysDictionary(platform));
        app.setDescription(description);
        app.setStorage(new SysDictionary(storage));
        app.setVisibility(new SysDictionary(visibility));
        app.setTags(tags);
        app.setFile(file);
        app.setImageFile(image);
        app.setDeveloper(developer);
        app.setDeveloperPhone(developerPhone);
        app.setVersion(version);
        app.setLinkdata(linkdata);

        app.setUpdater(getCurrentUser());
        app.setUpdateTime(new Date());


        app.setAuditStatus(new SysDictionary(181));
        app.setStatus(new SysDictionary(111));

        app.setFile(file);
        app.setImageFile(image);

        Result<App> result = service.modify(app, getRequest());
        return getReturnString(result, "redirect:/front/my/app/list", setMessage("操作失败", result.getMessage()));
    }


    @RequestMapping("remove")
    public String remove(@RequestParam("id") Integer id) {
        Result<App> result = service.remove(new App(id));
        return getReturnString(result, "redirect:/front/my/app/list", setMessage("操作失败", result.getMessage()));
    }


    @RequestMapping("view")
    public String detail(
            @RequestParam("id") Integer id
    ) {
        Result<App> result = service.getById(id);
        if (result.isSuccess()) {
            setRequestAttr("app", result.getTarget());
            return  "/front/my/app/view";
        } else {
            return setMessage("操作失败", "该应用不存在");
        }
    }
}
